package edu.upc.clase.demo.controller;

//import edu.upc.clase.demo.entity.Credential;
import edu.upc.clase.demo.entity.Promocion;
import edu.upc.clase.demo.entity.Cancha;
import edu.upc.clase.demo.service.PromocionService;
import edu.upc.clase.demo.service.CanchaService;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Six-Group-Upc
 */
@Controller("promocionController")
public class PromocionController {

    @Autowired
    private PromocionService promocionService;
    
    @Autowired
    private CanchaService canchaService;
    
    private static Logger log = LoggerFactory.getLogger(PromocionController.class);

    @RequestMapping("/promociones/index")
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView("promociones/index");
        List<Promocion> promociones = promocionService.buscarTodos();
        mav.addObject("promociones", promociones);
        return mav;
    }
    
    @RequestMapping(value = "/promociones/new", method = RequestMethod.GET)
    public ModelAndView newPromocion() {
        ModelAndView mav = new ModelAndView("promociones/new");
        Promocion promocion = new Promocion();
        mav.getModelMap().put("promocion", promocion);
        
        List<Cancha> canchas = canchaService.buscarTodos();
        mav.addObject("canchas", canchas);
        
        return mav;
    }

    @RequestMapping(value = "/promociones/new", method = RequestMethod.POST)
    public String createPromocion(@ModelAttribute("promocion")Promocion promocion, SessionStatus status) {    
        promocionService.insertar(promocion);
        status.setComplete();

        return "redirect:/pages/promociones/index";
        
    }
    
    @RequestMapping(value = "/promociones/edit", method = RequestMethod.GET)
    public ModelAndView editPromocion(@RequestParam("id")Integer id) {    
        ModelAndView mav = new ModelAndView("promociones/edit");
        Promocion promocion = promocionService.buscar(id);
        mav.getModelMap().put("promocion", promocion);
        
        List<Cancha> canchas = canchaService.buscarTodos();
        mav.addObject("canchas", canchas);
        
        return mav;        
    }
    
    @RequestMapping(value="/promociones/edit", method=RequestMethod.POST)
    public String update(@ModelAttribute("promocion") Promocion promocion, SessionStatus status) {
        promocionService.actualizar(promocion);
        status.setComplete();
        return "redirect:/pages/promociones/index";
    }
    
    @RequestMapping("/promociones/delete")
    public ModelAndView delete(@RequestParam("id")Integer id)
    {
        ModelAndView mav = new ModelAndView("redirect:/pages/promociones/index");
        Promocion promocion = promocionService.buscar(id);
        promocionService.eliminar(promocion);
        return mav;
    }
    
//    @RequestMapping("/locales/login")
//    public ModelAndView login() {
//        ModelAndView mav = new ModelAndView("locales/login");
//        Credential credential = new Credential();
//        mav.getModelMap().put("credential", credential);
//        return mav;
//    }    
//    
//    @RequestMapping(value = "/locales/autenticar", method=RequestMethod.POST)
//    public String autenticar(@ModelAttribute("credential") Credential credential,HttpServletRequest request) {
//        if (localService.autenticar(credential.getCorreo(), credential.getPassword())) {
//            request.getSession().setAttribute("credential",credential);
//            return "redirect:/pages/locales/index";
//        }
//        return "redirect:/pages/locales/login";
//    }
//    
//    @RequestMapping("/usuarios/logout")
//    public String logout(HttpServletRequest request) {
//        request.getSession().invalidate();
//        return "redirect:/pages/usuarios/login";
//    }        
//    
    
}
